package forum.dao;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import forum.bean.TextosBean;

public class TextosDAO implements Serializable {

	public boolean inserir(TextosBean textosBean) {
		try {
			SessionFactory sessionFactory = Sessao.getSessionFactory();
			Session session = sessionFactory.openSession();
			Transaction transaction = session.beginTransaction();
			
			session.save(textosBean);
			
			transaction.commit();
			session.close();
			
			return true;
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return false;
	}
	
	public List<String> nomesArquivos() {
		try {
			SessionFactory sessionFactory = Sessao.getSessionFactory();
			Session session = sessionFactory.openSession();
			
			List<String> lista = session.createSQLQuery("SELECT * FROM FORUM.SF_NOMES()").list();
			
			session.close();
			
			if (lista.size() > 0) {
				return lista;
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return null;
	}
	
	public byte[] arquivo(String nome_arquivo) {
		try {
			SessionFactory sessionFactory = Sessao.getSessionFactory();
			Session session = sessionFactory.openSession();
			
			List<byte[]> arquivo = session.createSQLQuery("SELECT * FROM FORUM.SF_ARQUIVO(:NOME)")
								.setParameter("NOME", nome_arquivo)
								.list();
			
			session.close();
			
			if (arquivo.size() > 0) {
				return arquivo.get(0);
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return null;
	}
}
